package com.smart.community.complaint.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 投诉建议视图对象
 * 
 * @author Wu.Liang
 * @version 1.0.0
 * @since 2025-06-26
 */
@Data
@Schema(description = "投诉建议视图对象")
public class ComplaintVO {

    @Schema(description = "投诉ID")
    private Long id;

    @Schema(description = "投诉编号")
    private String complaintNo;

    @Schema(description = "用户ID")
    private Long userId;

    @Schema(description = "用户姓名")
    private String userName;

    @Schema(description = "居民ID")
    private Long residentId;

    @Schema(description = "居民姓名")
    private String residentName;

    @Schema(description = "社区ID")
    private Long communityId;

    @Schema(description = "社区名称")
    private String communityName;

    @Schema(description = "投诉标题")
    private String title;

    @Schema(description = "投诉内容")
    private String content;

    @Schema(description = "投诉描述")
    private String complaintContent;

    @Schema(description = "投诉分类编码")
    private String categoryCode;

    @Schema(description = "投诉分类名称")
    private String categoryName;

    @Schema(description = "投诉位置")
    private String complaintLocation;

    @Schema(description = "投诉类型")
    private Integer complaintType;

    @Schema(description = "投诉类型名称")
    private String complaintTypeName;

    @Schema(description = "投诉级别")
    private Integer complaintLevel;

    @Schema(description = "投诉级别名称")
    private String complaintLevelName;

    @Schema(description = "紧急程度")
    private Integer urgencyLevel;

    @Schema(description = "紧急程度名称")
    private String urgencyLevelName;

    @Schema(description = "投诉人ID")
    private Long complainantId;

    @Schema(description = "投诉人姓名")
    private String complainantName;

    @Schema(description = "投诉人电话")
    private String complainantPhone;

    @Schema(description = "联系电话")
    private String contactPhone;

    @Schema(description = "是否匿名投诉")
    private Boolean isAnonymous;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "状态名称")
    private String statusName;

    @Schema(description = "处理状态")
    private Integer handleStatus;

    @Schema(description = "处理状态名称")
    private String handleStatusName;

    @Schema(description = "处理人ID")
    private Long handlerId;

    @Schema(description = "处理人姓名")
    private String handlerName;

    @Schema(description = "分配给")
    private Long assignTo;

    @Schema(description = "分配人姓名")
    private String assignToName;

    @Schema(description = "提交时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime submitTime;

    @Schema(description = "分配时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime assignTime;

    @Schema(description = "处理时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime handleTime;

    @Schema(description = "期望解决时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime expectedSolveTime;

    @Schema(description = "实际解决时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime actualSolveTime;

    @Schema(description = "回访时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime followupTime;

    @Schema(description = "处理步骤")
    private String processStep;

    @Schema(description = "处理结果")
    private String handleResult;

    @Schema(description = "解决方案")
    private String solution;

    @Schema(description = "回访结果")
    private String followupResult;

    @Schema(description = "满意度")
    private Integer satisfaction;

    @Schema(description = "满意度评分")
    private Integer satisfactionRating;

    @Schema(description = "满意度评分名称")
    private String satisfactionRatingName;

    @Schema(description = "图片列表")
    private List<String> images;

    @Schema(description = "备注")
    private String remark;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "是否超时")
    private Boolean isTimeout;

    @Schema(description = "处理时长（小时）")
    private Long processDuration;

    @Schema(description = "是否可处理")
    private Boolean canHandle;

    @Schema(description = "是否可关闭")
    private Boolean canClose;

    @Schema(description = "是否可回访")
    private Boolean canFollowup;

    @Schema(description = "是否可重开")
    private Boolean canReopen;

    /**
     * 获取紧急程度名称
     * 
     * @return 紧急程度名称
     */
    public String getUrgencyLevelName() {
        if (urgencyLevel == null) return null;
        
        switch (urgencyLevel) {
            case 1: return "低";
            case 2: return "中";
            case 3: return "高";
            case 4: return "紧急";
            default: return "未知";
        }
    }

    /**
     * 获取状态名称
     * 
     * @return 状态名称
     */
    public String getStatusName() {
        if (status == null) return null;
        
        switch (status) {
            case 0: return "待处理";
            case 1: return "处理中";
            case 2: return "已解决";
            case 3: return "已关闭";
            case 4: return "已重开";
            default: return "未知";
        }
    }

    /**
     * 获取处理状态名称
     * 
     * @return 处理状态名称
     */
    public String getHandleStatusName() {
        if (handleStatus == null) return null;
        
        switch (handleStatus) {
            case 0: return "未分配";
            case 1: return "已分配";
            case 2: return "处理中";
            case 3: return "已完成";
            default: return "未知";
        }
    }

    /**
     * 获取满意度评分名称
     * 
     * @return 满意度评分名称
     */
    public String getSatisfactionRatingName() {
        if (satisfactionRating == null) return null;
        
        switch (satisfactionRating) {
            case 1: return "非常不满意";
            case 2: return "不满意";
            case 3: return "一般";
            case 4: return "满意";
            case 5: return "非常满意";
            default: return "未知";
        }
    }

    /**
     * 检查是否超时
     * 
     * @return 是否超时
     */
    public Boolean getIsTimeout() {
        if (expectedSolveTime == null || status == null) {
            return false;
        }
        
        // 只有待处理和处理中的投诉才计算超时
        if (status == 0 || status == 1) {
            LocalDateTime now = LocalDateTime.now();
            return now.isAfter(expectedSolveTime);
        }
        
        return false;
    }

    /**
     * 计算处理时长（小时）
     * 
     * @return 处理时长
     */
    public Long getProcessDuration() {
        if (submitTime == null || actualSolveTime == null) {
            return null;
        }
        
        return java.time.Duration.between(submitTime, actualSolveTime).toHours();
    }

    /**
     * 检查是否可以处理
     * 
     * @return 是否可以处理
     */
    public Boolean getCanHandle() {
        return status != null && (status == 0 || status == 1);
    }

    /**
     * 检查是否可以关闭
     * 
     * @return 是否可以关闭
     */
    public Boolean getCanClose() {
        return status != null && status == 2;
    }

    /**
     * 检查是否可以回访
     * 
     * @return 是否可以回访
     */
    public Boolean getCanFollowup() {
        return status != null && status == 2 && satisfactionRating == null;
    }

    /**
     * 检查是否可以重开
     * 
     * @return 是否可以重开
     */
    public Boolean getCanReopen() {
        return status != null && status == 3;
    }
}
